package com.ts.api.module.match.plan.internal.distribute.strategy;

import com.gitee.apanlh.exp.ParamParserException;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.id.IdUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.common.constant.productflow.ProductApplyFlowStatusEnum;
import com.ts.api.module.api.convert.core.http.client.ApiHttpMessage;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistribute;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeField;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributePost;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeResult;
import com.ts.api.module.product.entity.po.ProductApplyFlowPO;
import com.ts.api.module.product.entity.po.ProductPO;
import com.ts.api.module.product.entity.po.ProductPlanPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  API渠道-机构计划分发-掩码模式
 *
 *  @author Pan
 */
@Service
public class ApiOrgPhoneMaskDistribute implements PlanDistributeStrategy<ApiOrgPhoneMaskConvertHandler> {

    @Autowired
    private BaseDistribute baseDistribute;

    @Override
    public PlanDistributeResult preValidate(PlanDistribute planDistribute) {
        return baseDistribute.preValidate(planDistribute);
    }

    @Override
    public PlanDistributeResult distribute(PlanDistribute planDistribute) {
        return baseDistribute.distribute(planDistribute);
    }

    @Override
    public PlanDistributeField parseResponse(PlanDistribute planDistribute, ApiHttpMessage apiHttpMessage, ApiOrgPhoneMaskConvertHandler handler) throws Exception {
        String reqId = planDistribute.getReqId();
        ProductPO productPO = planDistribute.getProductPO();
        ProductPlanPO productPlanPO = planDistribute.getProductPlanPO();
        ProductApplyFlowPO currentDistributeFlow = planDistribute.getCurrentDistributeFlow();
        String responseBody = apiHttpMessage.getResponseBody();

        //  验证非空情况
        if (ValidParam.isEmpty(responseBody)) {
            throw new ParamParserException("解析失败-响应体返回空");
        }

        //  解析响应体
        RespOrgPhoneMaskDataExchange resp = handler.respDataExchange(responseBody);
        if (ValidParam.isNull(resp)) {
            throw new ParamParserException("撞库参数异常:API机构-掩码模式未返回[RespOrgPhoneMaskDataExchange]请检查");
        }

        //  验证返回业务消息是否成功
        if (!resp.getHasBizSuc()) {
            return PlanDistributeField.ofFail("API机构-掩码模式-处理响应结果-对方返回结果[失败]", ProductApplyFlowStatusEnum.DATA_EXCHANGE_FAIL);
        }

        //  撞库流程http响应保存->序列化解析对象
        currentDistributeFlow.setRespData(JsonUtils.toJson(resp));

        //  构建解析后实体
        List<String> listPhoneMd5 = resp.getListPhoneMd5();
        return PlanDistributeField.ofOrgPhoneMask(productPO, productPlanPO, listPhoneMd5, reqId, IdUtils.getSimpleUuid());
    }

    @Override
    public PlanDistributeResult post(PlanDistributePost planDistributePost) {
        return PlanDistributeResult.ofSuc(planDistributePost);
    }
}
